#pragma once

#include <QWidget>
#include <QThread>
#include <QLineEdit>
#include <QPushButton>
#include "CClientPull.h"
#include "decode.h"
#include "VideoWaiter.h"
#include "AudioRenderer.h"
#include "OpenGLWidget.h"
#include <QTimerEvent>

class CWidget : public QWidget
{
    Q_OBJECT

public:
    CWidget(QWidget *parent = nullptr);
    ~CWidget();

protected:
    void timerEvent(QTimerEvent *event) override;

private:
    QLineEdit *lineEdit{nullptr};
    QPushButton *btnOpen{nullptr};
    QPushButton *btnClose{nullptr};

    CPullClient *rtspClient{nullptr};
    QThread *clientThread{nullptr};

    RtpDecoder *rtpDecoder{nullptr};
    QThread *decodeThread{nullptr};

    QWidget *w{nullptr};
    BaseOpenGLWidget *openglWidget{nullptr};
    VideoWaiter *videoWaiter{nullptr};
    QThread *videoThread{nullptr};

    AudioRenderer *audioRenderer{nullptr};
    QThread *audioThread{nullptr};

    void initThread(QObject *obj, QThread *thread);
    void destroyThread(QObject *obj, QThread *thread);

private slots:
    void onConnected();
    void onDisconnected();

    void onBtnOpenClicked();
    void onBtnCloseClicked();

    void onInitVideoOutput(int format);
    void receviceFrame(uint8_t *pixelData, int pixelWidth, int pixelHeight);

signals:
    void startConnect(const QString &url);
    void endConnect();
};